<template>
  <div class="page-content">
    <wj-table
      :dataList="dataList"
      :listCount="listCount"
      v-bind="contentTableConfig"
      v-model:page="pageInfo"
    >
      <!-- header插槽 -->
      <template #headerHeadler>
        <el-button v-if="isCreate" type="primary" size="mini" @click="handleNewClick"
          >新建</el-button
        >
        <el-button @click="getPageData()" icon="el-icon-refresh" size="mini" />
      </template>

      <!-- table内容插槽 -->
      <template #createAt="scope">
        <span>{{ $filters.formatTime(scope.row.createAt) }}</span>
      </template>
      <template #updateAt="scope">
        <span>{{ $filters.formatTime(scope.row.updateAt) }}</span>
      </template>
      <template #handler="scope">
        <div class="handle-btns">
          <el-button
            v-if="isUpdate"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleEditClick(scope.row)"
            >编辑</el-button
          >
          <el-button
            v-if="isDelete"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDeleteClick(scope.row)"
            >删除</el-button
          >
        </div>
      </template>

      <!-- 动态插槽 -->
      <template v-for="item in otherPropSlots" :key="item.prop" #[item.slotName]="scope">
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
    </wj-table>

    <!-- 删除弹出框 -->
    <el-dialog title="删除" v-model="dialogVisible" width="30%" center>
      <div v-html="dialogMessage"></div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleDelete">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, watch } from 'vue'
import WjTable from '@/base-ui/wj-table'
import { useStore } from '@/store'
import { usePermissions } from '@/hooks/use-permissions'

export default defineComponent({
  props: {
    contentTableConfig: {
      type: Object,
      required: true
    },
    pageName: {
      type: String,
      required: true
    }
  },
  components: {
    WjTable
  },
  emits: ['newBtnClick', 'editBtnClick'],
  setup(props, { emit }) {
    const store = useStore()
    const deleteItem = ref()
    const dialogVisible = ref(false)
    const dialogMessage = ref('')

    // 获取用户操作权限
    const isCreate = usePermissions(props.pageName, 'create')
    const isUpdate = usePermissions(props.pageName, 'update')
    const isDelete = usePermissions(props.pageName, 'delete')
    // const isQuery = usePermissions(props.pageName, 'query')

    // 双向绑定pageInfo
    const pageInfo = ref({ currentPage: 1, pageSize: 10 })

    watch(pageInfo, () => getPageData())

    // 发送网络请求
    const getPageData = (queryInfo: any = {}) => {
      // if (!isQuery) return
      store.dispatch('system/getPageListAction', {
        pageName: props.pageName,
        queryInfo: {
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize,
          ...queryInfo
        }
      })
    }

    getPageData()

    // 获取数据
    const dataList = computed(() => store.getters['system/pageListData'](props.pageName))
    const listCount = computed(() => store.getters['system/pageListCount'](props.pageName))

    // 获取其他动态插槽名称
    const otherPropSlots = props.contentTableConfig?.propList.filter((item: any) => {
      if (['createAt', 'updateAt', 'handler'].includes(item.slotName)) return false
      return true
    })

    // 删除操作
    const handleDeleteClick = (item: any) => {
      dialogMessage.value = `
        <span>确定删除用户 ${item.name} ?</span>
      `
      dialogVisible.value = true
      deleteItem.value = item
    }

    // 新建操作
    const handleNewClick = () => {
      emit('newBtnClick')
    }
    // 编辑操作
    const handleEditClick = (item: any) => {
      emit('editBtnClick', item)
    }

    // 删除请求
    const handleDelete = () => {
      dialogVisible.value = false
      store.dispatch('system/deletePageDataAction', {
        pageName: props.pageName,
        id: deleteItem.value.id
      })
    }

    return {
      dataList,
      listCount,
      getPageData,
      pageInfo,
      otherPropSlots,
      isCreate,
      isUpdate,
      isDelete,
      handleDeleteClick,
      handleNewClick,
      handleEditClick,
      dialogVisible,
      dialogMessage,
      handleDelete
    }
  }
})
</script>

<style lang="less" scoped>
.page-content {
  padding: 20px;
  border-top: 20px solid #f5f5f5;
}
</style>
